/**
 * @author lonun
 * @blog http://www.lonun.com
 */

var fs = require('fs');
var config = require(process.cwd() + '/config');
var models = require('../model/models');

exports.index = function(form) {
	return {};
}

exports.newPicture = function(form) {
	return {
		title : '上传图片'
	};
};

exports.savePicture = function(form) {
	var _files = [];
	for ( var f in form.files) {
		var tmpF = form.files[f];
		var d = new Date();
		var tempName = tmpF.name;
		var suffix = '';
		if (tempName.indexOf('.') > -1) {
			suffix = tempName.substring(tempName.lastIndexOf('.'));
		}
		var fname = '' + d.getFullYear() + (d.getMonth() + 1) + d.getDay()
				+ d.getHours() + d.getMinutes() + d.getSeconds()
				+ Math.random().toString().substring(2);
		fs.renameSync(tmpF.path, config.uploadDir + '/' + fname + suffix);
		_files[_files.length] = fname + suffix;
	}
	for ( var _f in _files) {
		var Pic = models.get('Picture');
		var pic = new Pic();
		pic.title = 'c pic';
		pic.filePath = _files[_f];
		pic.describtion = 'no desc.';
		pic.createDate = new Date();
		pic.save(function(err) {
			console.log(err);
		});
	}
}